<?php
namespace app\controllers\search;

use fate\http\Request;
use app\models\Posts;
use app\libs\Page;
use app\libs\Util;

class IndexController extends \fate\web\Controller {
    public function run() {
        $req = Request::getInstance();
        $k = Util::plainText($req->getQueryString('k'));
        $p = $req->getQueryString('p');

        if(!is_numeric($p)) {
            $p = 1;
        }

        // data
        $post = new Posts();
        $pageSize = 20;
        $where = "status = 1 and instr(title, '{$k}')";
        $total = $post->count($where);

        $pageString = (new Page($total, $pageSize))->show();
        $data = $post->getListByPage(
            'id,uid,tags,title,thumb,brief,views,post_time,status',
            $where,
            $p,
            $pageSize);

        //echo \fate\db\Db::instance('main')->getLastSql(); exit;
        $this->getView()->enableLayout = true;
        $this->render('@themes/search/index', [
            'list' => $data,
            'p' => $p,
            'pageSize' => $pageSize,
            'total' => $total,
            'pageString' => $pageString,
        ]);
    }
}
